package com.zzl.car.controller;

import com.zzl.car.entity.CarBaseInfo;
import com.zzl.car.entity.StoreInfo;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.service.ZzwStoreInfoService;
import com.zzl.car.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;


@CrossOrigin
@RestController
public class ZzwStoreInfoController {

    @Autowired
    private ZzwStoreInfoService storeInfoService;

    @Autowired
    private OssUtil ossUtil;

    /**
     * 上传文件到OSS
     *
     * @param file 上传的文件
     * @return 上传响应
     */
    @PostMapping("/uploadImage")
    public String fileUpload(@RequestBody MultipartFile file) {
        String originName = file.getOriginalFilename();
        System.out.println(originName);

        String url;
        //上传文件到OSS
        try {
            url = ossUtil.uploadFile(file,"StoreInfo");
            System.out.println("图片路径" + url);

        } catch (IOException e) {
            throw new ZdpException(2904,"图片上传失败");
        }

        if(storeInfoService.modifyStoreImage((url))){
            return "url";
        }else{
            return "修改失败";
        }
    }

    /**
     * 获取店面信息
     * @return StoreInfo对象
     */
    @PostMapping("/getStoreInfo")
    public StoreInfo getStoreInfo(){
        return storeInfoService.getStoreInfo();
    }

    /**
     * 修改店面信息
     * @return 也是返回一个 StoreInfo 对象
     */
    @PostMapping("/modifyStoreInfo")
    public int modifyStoreInfo(@RequestBody Map map){

        StoreInfo s = new StoreInfo();
        s.setName(map.get("name").toString());
        s.setAddress(map.get("address").toString());
        s.setManagerName(map.get("managerName").toString());
        s.setTelephone(map.get("telephone").toString());
        int result = storeInfoService.modifyStoreInfo(s);

        if(result == 0){
            return 0;
            // throw new ZdpException(2901,"modifyStoreInfo fail");
        }else{
            return 1;
        }
    }
}
